
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.classcompatibilityinspector;

import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class CCIResultTextWriter {
  private PrintWriter pw = null;
  private Map results = null;
  private static String lsep = System.getProperty("line.separator");

  public CCIResultTextWriter(Map m, Writer w) throws Exception {
    if(w instanceof PrintWriter) {
      pw = (PrintWriter)w;
    } else {
      pw = new PrintWriter(w);
    }
    //this.results = results;
    writeBody();
  }

  private void writeBody() throws Exception {

    StringBuffer buf = new StringBuffer();
    Comparator comp = new Comparator() {
        public int compare(Object o1, Object o2) {
          URL c1 = (URL)o1;
          URL c2 = (URL)o2;
          return c1.toString().compareTo(c2.toString());
        }
        
      };
    Set s = new TreeSet(comp);
    s.addAll(results.keySet());
    
    buf = new StringBuffer()
      .append("").append(lsep)
      .append("<FORM>").append(lsep)
      .append("<select name=select onchange=selectFile(this.options[this.selectedIndex].value) size=1 >").append(lsep)
      .append("<OPTION>------------------------------</OPTION>").append(lsep);
    pw.println(buf.toString());
    for(Iterator itr = s.iterator(); itr.hasNext(); ) {
      URL classurl = (URL)itr.next();
      pw.println("<OPTION value=#" + classurl + ">" + classurl + "</OPTION>");
    }
    pw.println("</select>");
    pw.println("</form>");
    
    for(Iterator itr = s.iterator(); itr.hasNext(); ) {
      URL classurl = (URL)itr.next();
      CCIResults cir = (CCIResults)results.get(classurl);
      buf = new StringBuffer()
        .append("<tr bgcolor=#CCCCCC>").append(lsep)
        .append("<td colspan=2> <a href=#top>top</a>")
        .append("<a name=").append(classurl).append("></a>")
        .append(" <b>").append(classurl).append("</b></td>").append(lsep)
        .append("</tr>").append(lsep);
      pw.println(buf.toString());
      String[] internalApis = cir.getInternalApis();
      String[] deprecatedApis = cir.getDeprecatedApis();
      String[] unavailableApis = cir.getUnavailableApis();
      addApis(internalApis, "Internal");
      addApis(deprecatedApis, "Deprecated");
      addApis(unavailableApis, "Unavailable");
    }
    
  }

  private void addApis(String[] apis, String description) {
    if(apis.length > 0) {
      StringBuffer buf = new StringBuffer();
      buf.append("<tr bgcolor=#FFFFCC>").append(lsep)
        .append("<td valign=top><i>")
        .append(description).append(" Apis</i></td>").append(lsep)
        .append("<td>");
      for(int i = 0; i < apis.length; i++) {
        buf.append(apis[i]).append("<br>").append(lsep);
      }
      buf.append("</td>").append(lsep).append("</tr>").append(lsep);
      pw.println(buf.toString());
    }
  }
                     

}
